/**
 * @Author: Liu, Yunfeng
 * @DateTime: 2021/7/16 20:53
 * @Description: TODO
 */
public class searchCountInNumber {
    public static int search(int[] nums, int target) {
        int count=0;
        int index = binarySearch(nums, target);
        if(index == -1){
            return 0;
        }
        for(int i=index;i>=0;i--){
            if(nums[i]!= target){
                break;
            }
            count++;
        }
        for(int i= index; i< nums.length;i++){
            if(nums[i]!= target){
                break;
            }
            count++;
        }
        return count-1;
    }

    private static int  binarySearch(int[] nums, int target){
        if (nums == null || nums.length == 0) {
            return -10;
        }
        int start = 0;
        int end = nums.length - 1;
        int mid=0;
        while (start <= end) {
            mid = (start + end) / 2;
            if (nums[mid] == target){
                return mid;
            }else if(nums[mid] > target){
                end = mid-1;
            }else{
                start = mid+1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {5,7,7,8,8,10};
        int target = 10;
        System.out.println(search(nums,target));
    }
}
